package com.wx.cloud.permission.controller;

import com.wx.cloud.permission.common.CommonResult;
import com.wx.cloud.permission.model.Page;
import com.wx.cloud.permission.model.Role;
import com.wx.cloud.permission.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/getRoleList")
    public CommonResult getRoleList(){
        HashMap<String, Object> map = new HashMap<>();
        List<Role> roles = roleService.getRoleList();
        map.put("data",roles);
        return CommonResult.success(map);
    }

    @GetMapping("/getSelectList")
    public CommonResult getSelectList(@RequestParam Map<String, Object> data){
        HashMap<String, Object> map = new HashMap<>();
        List<Role> roles = roleService.getSelectList(data);
        int count = roleService.getSelectCount(data);
        Page page = new Page();
        page.setList(roles);
        page.setTotal(count);
        map.put("list",roles);
        map.put("count",count);
        return CommonResult.success(map);
    }

    @GetMapping("/getRoleById")
    public CommonResult getRoleById(@RequestParam Integer id){
        HashMap<String, Object> map = new HashMap<>();
        Role role = roleService.getRoleById(id);
        map.put("data",role);
        return CommonResult.success(map);
    }

    @PostMapping("/addRole")
    public CommonResult addRole(@RequestBody Role role){
        HashMap<String, String> map = new HashMap<>();
        String result = roleService.addRole(role);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PostMapping("/updateRole")
    public CommonResult updateRole(@RequestBody Role role){
        HashMap<String, Object> map = new HashMap<>();
        int result = roleService.updateRole(role);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PostMapping("/delRole")
    public CommonResult delRole(@RequestBody Role role){
        HashMap<String, Integer> map = new HashMap<>();
        int result = roleService.delRole(role);
        map.put("result",result);
        return CommonResult.success(map);
    }

    @PostMapping("/updateState")
    public CommonResult updateState(@RequestBody Role role){
        HashMap<String, Object> map = new HashMap<>();
        int result = roleService.updateState(role);
        map.put("result",result);
        return CommonResult.success(map);
    }

}
